package com.itheima.tanhua.api;

import com.itheima.tanhua.api.mongo.RecommendServiceApi;
import com.itheima.tanhuan.pojo.mongo.RecommendUser;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@DubboService
public class RecommendServiceApiImpl implements RecommendServiceApi {

    @Autowired
    private MongoTemplate mongoTemplate;
    /**
     * 1、查询今日佳人
     * @param toUserId
     * @return
     */
    @Override
    public RecommendUser todayBest(Long toUserId) {
        //组装查询条件
        Query query=Query.query(Criteria.where("toUserId").is(toUserId))
                          .with(Sort.by(Sort.Order.desc("score"))) //按照缘分值倒序排列
                          .limit(1);//取第一条
        return mongoTemplate.findOne(query, RecommendUser.class);
    }

    /**
     * 2、用uid到mongo中查询所有佳人信息
     *
     * @param uid
     * @return
     */
    @Override
    public List<RecommendUser> findByToUid(Long uid) {
        Query query=Query.query(Criteria.where("toUserId").is(uid))
                .with(Sort.by(Sort.Order.desc("score"))); //按照缘分值倒序排列
        return mongoTemplate.find(query,RecommendUser.class);
    }
}
